using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;

public class GameSceneManager : Singleton<GameSceneManager>
{
    [SerializeField] private Transform uiParent;
    [SerializeField] private Text tips;
    private Dictionary<string, string[]> sceneUIDict = new Dictionary<string, string[]>();
    private bool isRest = false;
    public List<string> levels = new List<string>();

    protected override void Awake()
    {
        base.Awake();
        levels.Add("Level01");
        sceneUIDict.Add("Scene_Home", new string[] { "PlayerProperty", "Home" });
        sceneUIDict.Add("LevelSelect", new string[] { "LevelSelect", "ReturnHome" });
        sceneUIDict.Add("Level01", new string[] { "PlayerProperty", "ReturnHome" });
        sceneUIDict.Add("Level02", new string[] { "PlayerProperty", "ReturnHome" });
        sceneUIDict.Add("LevelEnd", new string[] { "MissionEnd" });
        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        if ("StartScene".Equals(scene.name))
        {
            return;
        }
        if(isRest && "Scene_Home".Equals(scene.name))
        {
            GameManager.GetInstance().UpdateDayCountAndDayState();
            GameManager.GetInstance().IncreasePlayerStrength(20f);
            isRest = false;
        }
        ClearUIRoots(sceneUIDict[scene.name]);
        UIFade.GetInstance().FadeToClear();
    }

    public void LoadSceneToHome()
    {
        if (GameManager.GetInstance().isBattling)
        {
            //从关卡中回家，更新白天黑夜和天数
            GameManager.GetInstance().UpdateDayCountAndDayState();
        }
        UIFade.GetInstance().FadeToBlack();
        GameManager.GetInstance().isBattling = false;
        StartCoroutine(LoadSceneRountine("Scene_Home"));
    }

    public void OnClickRest()
    {
        UIFade.GetInstance().FadeToBlack();
        isRest = true;
        StartCoroutine(LoadSceneRountine("Scene_Home"));
    }

    public void LoadSceneToLevelSelect()
    {
        UIFade.GetInstance().FadeToBlack();
        GameManager.GetInstance().isBattling = false;
        StartCoroutine(LoadSceneRountine("LevelSelect"));
    }

    public void LoadSceneToLevelEnd()
    {
        UIFade.GetInstance().FadeToBlack();
        GameManager.GetInstance().isBattling = false;
        StartCoroutine(LoadSceneRountine("LevelEnd"));
    }

    public void LoadSceneToLevel(string levelName)
    {
        int level = int.Parse(levelName.Replace("Level", ""));
        if(GameManager.GetInstance().dayCount < level)
        {
            //提示
            StartCoroutine(ShowTipsRountine("每天只能去一个关卡，休息一晚才能去下一关", 3f));
            return;
        }
        UIFade.GetInstance().FadeToBlack();
        GameManager.GetInstance().isBattling = true;
        StartCoroutine(LoadSceneRountine(levelName));
    }

    public IEnumerator ShowTipsRountine(string text, float time)
    {
        tips.text = text;
        yield return new WaitForSecondsRealtime(time);
        tips.text = "";
    }

    public IEnumerator LoadSceneRountine(string sceneToLoad)
    {
        yield return new WaitForSeconds(1.2f);
        SceneManager.LoadScene(sceneToLoad);
    }

    private void ClearUIRoots(string[] uiRoots)
    {
        foreach (Transform childTransform in uiParent)
        {
            if ("Tips".Equals(childTransform.name))
            {
                continue;
            }
            if ("UIFade".Equals(childTransform.name))
            {
                continue;
            }
            if ("EnemyHealthRoot".Equals(childTransform.name))
            {
                continue;
            }
            if (uiRoots.Contains(childTransform.name))
            {
                childTransform.gameObject.SetActive(true);
                if ("LevelSelect".Equals(childTransform.name))
                {
                    foreach(Transform levelButtons in childTransform)
                    {
                        if(levels.Contains(levelButtons.name.Replace("Button_", "")))
                        {
                            levelButtons.gameObject.SetActive(true);
                        }
                    }
                }
                continue;
            }
            childTransform.gameObject.SetActive(false);
        }
    }

}
